OPC Studio User's Guide and Reference
Getting Started with OPC Data Client and OPC UA PubSub using PyCharm
Getting Started > Getting Started with OPC Data Client > Getting Started with OPC Data Client in Python > Getting Started with OPC Data Client and OPC UA PubSub using PyCharm
In This Topic

Prerequisites

Console Application to Subscribe to OPC UA Datasets over UDP UADP

In this Getting Started, we will use PyCharm (on Windows) create a console application running that will subscribe to datasets produced by an OPC UA demo publisher, and display the received data on the console. 

  1. On the Welcome to PyCharm screen, press the New Project button.

  2. In the New Project dialog, leave all settings at their defaults, and press the Create button.

  3. If prompted by PyCharm, allow the Microsoft Defender configuration by pressing the Automatically button, and further allowing PyCharm to make corresponding changes to the system.

  4. Switch to the Terminal window of PyCharm, type pip install opclabs_quickopc, and press Enter. Wait until the package is fully installed.

  5. From the PyCharm menu, select File -> New, and then in the New menu, select Python file.

  6. In the New Python file window, type in helloPubSub for the Name, and press Enter.

  7. In the helloPubSub.py editor tab, add following code:

    import opclabs_quickopc
    from OpcLabs.EasyOpc.UA.PubSub import *
    
    def dataSetMessage(sender, eventArgs):
        if eventArgs.Succeeded:
            if eventArgs.DataSetData is not None:
                print('')
                print('Dataset data:', eventArgs.DataSetData)
                for pair in eventArgs.DataSetData.FieldDataDictionary:
                    print(pair)
        else:
            print('')
            print('*** Failure:', eventArgs.ErrorMessageBrief)
    
    subscriber = EasyUASubscriber()
    IEasyUASubscriberExtension.SubscribeDataSet(subscriber,
                                                UAPubSubConnectionDescriptor.op_Implicit('opc.udp://239.0.0.1'),
                                                EasyUADataSetMessageEventHandler(dataSetMessage))
    input()
    subscriber.UnsubscribeAllDataSets()
                                            
    
    You will likely get red squiggles underlining on the OpcLabs namespace in the "from ... import" statement, and on other identifiers further on in the code. Ignore it. This happens because OPC Data Client is based on Python.NET, and the Python IDE cannot recognize the symbols from dynamically imported namespaces.
  8. Visit Tool Downloads page and download and unpack OPC UA Demo Publisher (on Windows, the easiest to work with is ClickOnce, but other format will do as well). In accordance with instructions on the UADemoPublisher Basics page, run the OPC UA Demo Publisher without further parameters.

    If you have installed OPC Data Client using its full Setup program, you can bypass the tool download and installation described above, and simply run Tools -> OPC UA Demo Publisher from the Launcher application, or Windows Start menu.

    The OPC UA Demo Publisher will start broadcasting messages that our application will receive. You will see a progress indication (message counts) in its console window. You can run the OPC UA Demo Publisher either on the same computer where you are developing the application, or on a different computer on the same local network (IP subnet).

  9. Select Run -> Run 'helloPubSub.py' (Shift+F10) from the PyCharm menu, or press the corresponding button (green right-pointing hollow triangle) on the PyCharm toolbar.

    This will build and launch the program. The datasets from the publisher will be received by your subscriber application, and displayed on the console. Press Enter to exit the program.

Troubleshooting

See OPC UA PubSub Common Traps And Pitfalls if things do not work as expected - and specifically, if your subscriber application is not receiving anything and is giving you timeout errors.

Probably the most common issue is the necessity to properly specify a (non-default) network interface. For the OPC UA Demo Publisher, this is done using the --ConnectionNetworkInterface (-cni) option. For a suggestion about how this is done in your application, see e.g. the code comments in Examples - OPC UA PubSub - Callback using a lambda.

 

See Also

Fundamentals